/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.util;

import javax.swing.JOptionPane;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.ForEachPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.IfElsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.LoopNPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.WhilePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.ConditionalResponsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.CountLoopPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.ForEachInListSequentialLoopPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.LoopIfTrueResponsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.ParallelResponsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.SequentialResponsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.BooleanPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.CallToUserDefinedQuestionPrototypeDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.CallToUserDefinedResponsePrototypeDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.CollectionPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.DefaultPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementNamePropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementPrototypeDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.EnumerablePropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.FontPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.FormattedElementViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.KeyCodePropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.NumberPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.PropertyDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.PropertyGUI;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.PropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.QuestionPrintViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ResponsePrintViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ResponsePropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.StringPropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.StylePropertyViewController;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.VariableDnDPanel;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.VariableGUI;
import edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.VariablesViewController;
import edu.cmu.cs.stage3.resourceBundle.I18n;
import edu.cmu.cs.stage3.swing.DialogManager;
import edu.cmu.cs.stage3.util.Enumerable;

public class GUIFactory {

 public static java.util.HashMap guiCache = new java.util.HashMap();


 protected static CollectionEditorPanel collectionEditorPanel;

 public static CollectionEditorPanel getCollectionEditorPanel () {
  if (collectionEditorPanel == null) {
   collectionEditorPanel = new CollectionEditorPanel();
   }
  return collectionEditorPanel;
  }

 public static javax.swing.JComponent getGUI (Object o) {
  AuthoringTool authoringTool = AuthoringTool.getInstance();
  javax.swing.JComponent viewController = null;

  if (o instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) {
   viewController = getOrCreateGUI( ConditionalResponsePanel.class );
   if (viewController != null) {
    ((ConditionalResponsePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.IfElseInOrder)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.LoopNInOrder) {
   viewController = getOrCreateGUI( CountLoopPanel.class );
   if (viewController != null) {
    ((CountLoopPanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.LoopNInOrder)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.WhileLoopInOrder) {
   viewController = getOrCreateGUI( LoopIfTrueResponsePanel.class );
   if (viewController != null) {
    ((LoopIfTrueResponsePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.WhileLoopInOrder)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.ForEachInOrder) {
   viewController = getOrCreateGUI( ForEachInListSequentialLoopPanel.class );
   if (viewController != null) {
    ((ForEachInListSequentialLoopPanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.ForEachInOrder)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.DoTogether) {
   viewController = getOrCreateGUI( ParallelResponsePanel.class );
   if (viewController != null) {
    ((ParallelResponsePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.DoTogether)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.DoInOrder) {
   viewController = getOrCreateGUI( SequentialResponsePanel.class );
   if (viewController != null) {
    ((SequentialResponsePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.response.DoInOrder)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
   viewController = getOrCreateGUI( IfElsePanel.class );
   if (viewController != null) {
    ((IfElsePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.LoopN) {
   viewController = getOrCreateGUI( LoopNPanel.class );
   if (viewController != null) {
    ((LoopNPanel)viewController).set( (edu.cmu.cs.stage3.alice.core.question.userdefined.LoopN)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.While) {
   viewController = getOrCreateGUI( WhilePanel.class );
   if (viewController != null) {
    ((WhilePanel)viewController).set( (edu.cmu.cs.stage3.alice.core.question.userdefined.While)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.ForEach) {
   // System.out.println("GUIFactory.java: cria 'iterador de lista'");
   viewController = getOrCreateGUI( ForEachPanel.class );
   if (viewController != null) {
    ((ForEachPanel)viewController).set( (edu.cmu.cs.stage3.alice.core.question.userdefined.ForEach)o, authoringTool );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.response.Print) {
   viewController = getOrCreateGUI( ResponsePrintViewController.class );
   if (viewController != null) {
    ((ResponsePrintViewController)viewController).set( (edu.cmu.cs.stage3.alice.core.response.Print)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Print) {
   viewController = getOrCreateGUI( QuestionPrintViewController.class );
   if (viewController != null) {
    ((QuestionPrintViewController)viewController).set( (edu.cmu.cs.stage3.alice.core.question.userdefined.Print)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Component) {
   viewController = getOrCreateGUI( FormattedElementViewController.class );
   if (viewController != null) {
    ((FormattedElementViewController)viewController).setElement( (edu.cmu.cs.stage3.alice.core.Element)o );
    }
  } else if ((o instanceof edu.cmu.cs.stage3.alice.core.Response) || (o instanceof edu.cmu.cs.stage3.alice.core.Question)) {
   viewController = getOrCreateGUI( FormattedElementViewController.class );
   if (viewController != null) {
    ((FormattedElementViewController)viewController).setElement( (edu.cmu.cs.stage3.alice.core.Element)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedResponsePrototype) {
   viewController = getOrCreateGUI( CallToUserDefinedResponsePrototypeDnDPanel.class );
   if (viewController != null) {
    ((CallToUserDefinedResponsePrototypeDnDPanel)viewController).set( (edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedResponsePrototype)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedQuestionPrototype) {
   viewController = getOrCreateGUI( CallToUserDefinedQuestionPrototypeDnDPanel.class );
   if (viewController != null) {
    ((CallToUserDefinedQuestionPrototypeDnDPanel)viewController).set( (edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedQuestionPrototype)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.authoringtool.util.ElementPrototype) {
   viewController = getOrCreateGUI( ElementPrototypeDnDPanel.class );
   if (viewController != null) {
    ((ElementPrototypeDnDPanel)viewController).set( (edu.cmu.cs.stage3.alice.authoringtool.util.ElementPrototype)o );
    }
  } else if (o instanceof edu.cmu.cs.stage3.alice.core.Element) {
   viewController = getOrCreateGUI( ElementDnDPanel.class );
   if (viewController != null) {
    ((ElementDnDPanel)viewController).set( (edu.cmu.cs.stage3.alice.core.Element)o );
    }
   }
  return viewController;
  }

 public static EditObjectButton getEditObjectButton (Object object, javax.swing.JComponent animationSource) {
  AuthoringTool authoringTool = AuthoringTool.getInstance();
  EditObjectButton editObjectButton = (EditObjectButton)getOrCreateGUI( EditObjectButton.class );
  editObjectButton.setAuthoringTool( authoringTool );
  editObjectButton.setObject( object );
  editObjectButton.setAnimationSource( animationSource );
  return editObjectButton;
  }

 public static void releaseGUI (edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement guiElement) {
  //  guiElement.die();
  guiElement.clean();
  Class guiClass = guiElement.getClass();
  java.util.Set guiSet = (java.util.Set)guiCache.get( guiClass );
  if (guiSet == null) {
   guiSet = new java.util.HashSet();
   guiCache.put( guiClass, guiSet );
   }
  guiSet.add( guiElement );
  }

 protected static javax.swing.JComponent getOrCreateGUI (Class guiClass) {
  java.util.Set guiSet = (java.util.Set)guiCache.get( guiClass );
  if ((guiSet != null) && (! guiSet.isEmpty())) {
   javax.swing.JComponent guiElement = (javax.swing.JComponent)guiSet.iterator().next();
   if (guiElement.getParent() != null) {
    guiElement.getParent().remove( guiElement );
    }
   guiSet.remove( guiElement );
   return guiElement;
  } else {
   try {
    return (javax.swing.JComponent)guiClass.newInstance();
   } catch( Throwable t) {
    AuthoringTool.getInstance().showErrorDialog( "Error creating new GUI; " + guiClass, t );
    }
   }
  return null;
  }

 public static PropertyGUI getPropertyGUI (edu.cmu.cs.stage3.alice.core.Property property, boolean includeDefaults, boolean allowExpressions, PopupItemFactory factory) {
  PropertyGUI propertyGUI = (PropertyGUI)getOrCreateGUI( PropertyGUI.class );
  if (propertyGUI != null) {
   propertyGUI.set( AuthoringTool.getInstance(), property, includeDefaults, allowExpressions, factory );
   }
  return propertyGUI;
  }

 public static String cleanHTMLString (String toReturn) {
  if (toReturn == "") {return toReturn;}
  while (toReturn != null && toReturn.indexOf("<") > -1) {
   toReturn = toReturn.substring(0, toReturn.indexOf("<"))+ "&lt;"+toReturn.substring(toReturn.indexOf("<")+1, toReturn.length());
   }
  if (toReturn == "") {return toReturn;}
  while (toReturn != null && toReturn.indexOf(">") > -1) {
   toReturn = toReturn.substring(0, toReturn.indexOf(">"))+ "&gt;"+toReturn.substring(toReturn.indexOf(">")+1, toReturn.length());
   }
  if (toReturn == "") {return toReturn;}
  while (toReturn != null &&  toReturn.indexOf("\"") > -1) {
   toReturn = toReturn.substring(0, toReturn.indexOf("\""))+ "&quot;"+toReturn.substring(toReturn.indexOf("\"")+1, toReturn.length());
   }
  return toReturn;
  }

 public static String getHTMLStringForComponent (java.awt.Component c) {
  String toReturn = "";

  if (c instanceof javax.swing.JLabel) {
   if (((javax.swing.JLabel)c).getIcon() != null) {
    javax.swing.Icon icon = ((javax.swing.JLabel)c).getIcon();
    if (icon.equals(AuthoringToolResources.getIconForValue("mouse"))) {
     toReturn = "<b>the mouse</b>";
    } else if (icon.equals(AuthoringToolResources.getIconForValue("arrowKeys"))) {
     toReturn = "<b>the arrow keys</b>";
     } 
    }
   String labelText = cleanHTMLString(((javax.swing.JLabel)c).getText());
   if (((javax.swing.JLabel)c).getFont().isBold()) {
    toReturn += "<b>"+labelText+"</b>";
    }
   if (((javax.swing.JLabel)c).getFont().isItalic()) {
    toReturn += "<i>"+labelText+"</i>";
    }
   toReturn = " "+toReturn+" ";
   if (((javax.swing.JLabel)c).getText() == null || ((javax.swing.JLabel)c).getText().equals("more...")) {
    toReturn = "";
    }
  } else if (c instanceof PropertyViewController) {
   PropertyViewController viewControllerC = (PropertyViewController)c;
   StringBuffer htmlStringBuf = new StringBuffer();
   viewControllerC.getHTML(htmlStringBuf);
   toReturn = " "+htmlStringBuf.toString()+" ";
  } else if (c instanceof java.awt.Container) {
   java.awt.Container containerC = (java.awt.Container)c;
   for (int i=0; i<containerC.getComponentCount(); i++) {
    toReturn += getHTMLStringForComponent(containerC.getComponent(i))+" ";
    }
  } else {
    }
  toReturn = toReturn.trim();
  while ( toReturn.indexOf("  ") > -1) {
   int index = toReturn.indexOf("  ");
   toReturn = toReturn.substring(0, index)+toReturn.substring(index+1, toReturn.length());
   }

  return toReturn;
  }

 public static PropertyDnDPanel getPropertyDnDPanel (edu.cmu.cs.stage3.alice.core.Property property) {
  PropertyDnDPanel propertyDnDPanel = (PropertyDnDPanel)getOrCreateGUI( PropertyDnDPanel.class );
  if (propertyDnDPanel != null) {
   propertyDnDPanel.set( AuthoringTool.getInstance(), property );
   }
  return propertyDnDPanel;
  }

 /**
  * @deprecated  use getPropertyViewController
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createPropertyViewController (edu.cmu.cs.stage3.alice.core.Property property, boolean includeDefaults, boolean allowExpressions, boolean omitPropertyName, PopupItemFactory factory) {
  return getPropertyViewController( property, includeDefaults, allowExpressions, omitPropertyName, factory );
  }

 public static javax.swing.JComponent getPropertyViewController (edu.cmu.cs.stage3.alice.core.Property property, boolean includeDefaults, boolean allowExpressions, boolean omitPropertyName, PopupItemFactory factory) {
  javax.swing.JComponent viewController = null;
  Class desiredValueClass = edu.cmu.cs.stage3.alice.authoringtool.util.PopupMenuUtilities.getDesiredValueClass(property);

  if (property.getName().equals( "keyCode" ) && Integer.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( KeyCodePropertyViewController.class );
   if (viewController != null) {
    ((KeyCodePropertyViewController)viewController).set( property, allowExpressions, factory );
    }
  } else if (Boolean.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( BooleanPropertyViewController.class );
   if (viewController != null) {
    ((BooleanPropertyViewController)viewController).set( property, includeDefaults, allowExpressions, omitPropertyName, factory );
    }
  } else if (Number.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( NumberPropertyViewController.class ); //
   if (viewController != null) {
    ((NumberPropertyViewController)viewController).set( property, includeDefaults, allowExpressions, omitPropertyName, factory );
    }
  } else if (edu.cmu.cs.stage3.alice.core.Response.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( ResponsePropertyViewController.class );
   if (viewController != null) {
    ((ResponsePropertyViewController)viewController).set( property, allowExpressions, omitPropertyName, factory );

    }
  } else if (String.class.isAssignableFrom( desiredValueClass )) {
   boolean emptyStringWritesNull = true;
   if (property.getName().equals( "script" )) {
    allowExpressions = false;
    emptyStringWritesNull = false;
    }
   viewController = getOrCreateGUI( StringPropertyViewController.class );
   if (viewController != null) {
    ((StringPropertyViewController)viewController).set( property, includeDefaults, allowExpressions, omitPropertyName, emptyStringWritesNull, factory );
    }
  } else if (java.awt.Font.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( FontPropertyViewController.class );
   if (viewController != null) {
    ((FontPropertyViewController)viewController).set( property, omitPropertyName );
    }
  } else if (edu.cmu.cs.stage3.alice.core.Style.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( StylePropertyViewController.class );
   if (viewController != null) {
    ((StylePropertyViewController)viewController).set( property, allowExpressions, omitPropertyName, factory );
    }
  } else if (Enumerable.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( EnumerablePropertyViewController.class );
   if (viewController != null) {
    ((EnumerablePropertyViewController)viewController).set( property, allowExpressions, omitPropertyName, factory );
    }
  } else if (edu.cmu.cs.stage3.alice.core.Collection.class.isAssignableFrom( desiredValueClass )) {
   if (allowExpressions) {
    viewController = getOrCreateGUI( ElementPropertyViewController.class );
    if (viewController != null) {
     ((ElementPropertyViewController)viewController).set( property, allowExpressions, omitPropertyName, factory );
     }
   } else {
    viewController = getOrCreateGUI( CollectionPropertyViewController.class );
    if (viewController != null) {
     ((CollectionPropertyViewController)viewController).set( property, omitPropertyName );
     }
    }
  } else if (edu.cmu.cs.stage3.alice.core.Element.class.isAssignableFrom( desiredValueClass )) {
   viewController = getOrCreateGUI( ElementPropertyViewController.class );
   if (viewController != null) {
    ((ElementPropertyViewController)viewController).set( property, allowExpressions, omitPropertyName, factory );
    }
  } else if (property instanceof edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty) {
   edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty oap = (edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty)property;
   if (edu.cmu.cs.stage3.alice.core.Variable.class.isAssignableFrom( oap.getComponentType() )) {
    viewController = getOrCreateGUI( VariablesViewController.class );
    if (viewController != null) {
     ((VariablesViewController)viewController).set( oap );
     }
    }
  } else {
   viewController = getOrCreateGUI( DefaultPropertyViewController.class );
   if (viewController != null) {
    ((DefaultPropertyViewController)viewController).set( property, includeDefaults, allowExpressions, false, omitPropertyName, factory );
    }
   }

  return viewController;
  }

 /**
  * @deprecated use getVariableGUI
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createVariableGUI( edu.cmu.cs.stage3.alice.core.Variable variable, boolean includeDefaults, PopupItemFactory factory) {
  return getVariableGUI( variable, includeDefaults, factory );
  }

 /**
  * @deprecated use getVariableDnDPanel
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createVariableDnDPanel (edu.cmu.cs.stage3.alice.core.Variable variable) {
  return getVariableDnDPanel( variable );
  }

 public static VariableGUI getVariableGUI (edu.cmu.cs.stage3.alice.core.Variable variable, boolean includeDefaults, PopupItemFactory factory) {
  VariableGUI variableGUI = (VariableGUI)getOrCreateGUI( VariableGUI.class );
  if (variableGUI != null) {
   variableGUI.set( AuthoringTool.getInstance(), variable, includeDefaults, factory );
   }
  return variableGUI;
  }


 // edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.VariableGUI.java: when a variable is created
 // + void set(AuthoringTool authoringTool, edu.cmu.cs.stage3.alice.core.Variable variable, boolean includeDefaults, edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory):
 //   variableDnDPanel = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getVariableDnDPanel( variable );
 public static VariableDnDPanel getVariableDnDPanel (edu.cmu.cs.stage3.alice.core.Variable variable) {
  VariableDnDPanel variableDnDPanel = (VariableDnDPanel)getOrCreateGUI( VariableDnDPanel.class );
  if (variableDnDPanel != null) {
   variableDnDPanel.set( AuthoringTool.getInstance(), variable ); // called by: VariableGUI.set(AuthoringTool,...) when a variable is created
   }
  return variableDnDPanel;
  }

 /**
  * @deprecated  use getGUI( Object )
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createResponseViewController (edu.cmu.cs.stage3.alice.core.Response response) {
  return getGUI( response );
  }

 /**
  * @deprecated  use getGUI( Object )
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createQuestionViewController (edu.cmu.cs.stage3.alice.core.Question question) {
  return getGUI( question );
  }

 /**
  * @deprecated  use getGUI( Object )
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createElementPrototypeDnDPanel (edu.cmu.cs.stage3.alice.authoringtool.util.ElementPrototype elementPrototype) {
  return getGUI( elementPrototype );
  }

 /**
  * @deprecated  use getGUI( Object )
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createResponsePrototypeDnDPanel (edu.cmu.cs.stage3.alice.authoringtool.util.ResponsePrototype responsePrototype) {
  return getGUI( responsePrototype );
  }

 /**
  * @deprecated  use getGUI( Object )
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createCallToUserDefinedResponsePrototypeDnDPanel (edu.cmu.cs.stage3.alice.authoringtool.util.CallToUserDefinedResponsePrototype callToUserDefinedResponsePrototype) {
  return getGUI( callToUserDefinedResponsePrototype );
  }

 /**
  * @deprecated  use getElementNamePropertyViewController
  */
 //Rem @Deprecated
 public static javax.swing.JComponent createRightClickNameEditor (edu.cmu.cs.stage3.alice.core.Element element) {
  return getElementNamePropertyViewController( element );
  }

 public static ElementNamePropertyViewController getElementNamePropertyViewController (edu.cmu.cs.stage3.alice.core.Element element) {
  ElementNamePropertyViewController enpvc = (ElementNamePropertyViewController)getOrCreateGUI( ElementNamePropertyViewController.class );
  enpvc.set( element );
  return enpvc;
  }

 public static ElementDnDPanel getElementDnDPanel (edu.cmu.cs.stage3.alice.core.Element element) {
  ElementDnDPanel panel = (ElementDnDPanel)getOrCreateGUI( ElementDnDPanel.class );
  panel.set( element );
  return panel;
  }

 public static boolean isOtherDialogSupportedForClass (Class valueClass) {
  if (Number.class.isAssignableFrom( valueClass )) {
   return true;
  } else if (edu.cmu.cs.stage3.alice.core.Style.class.isAssignableFrom( valueClass )) {
   return false; //TODO: true
  } else if (String.class.isAssignableFrom( valueClass )) {
   return true;
  } else if (edu.cmu.cs.stage3.alice.core.Response.class.isAssignableFrom( valueClass )) {
   return true;
  } else {
   return false;
   }
  }

 public static void showOtherPropertyDialog (edu.cmu.cs.stage3.alice.core.Property property, PopupItemFactory factory) {
  showOtherPropertyDialog( property, factory, null );
  }

 public static void showOtherPropertyDialog (edu.cmu.cs.stage3.alice.core.Property property, PopupItemFactory factory, java.awt.Point location) {
  showOtherPropertyDialog( property, factory, location, null );
  }

 public static void showOtherPropertyDialog (edu.cmu.cs.stage3.alice.core.Property property, PopupItemFactory factory, java.awt.Point location, Class valueClass) {
  if (valueClass == null) {
   valueClass = property.getValueClass();
   }
  if (Number.class.isAssignableFrom( valueClass )) {
   Object currentValue = property.getValue();
   String currentString = "";
   if (currentValue != null) {
    currentString = currentValue.toString();
    }
   String stringTyped = (String) DialogManager.showInputDialog(I18n.getString("enterNumber")+":", I18n.getString("enterNumber"),
     JOptionPane.PLAIN_MESSAGE, null, null, currentString, true);
   if (stringTyped != null) {
    //L double number = Double.valueOf(stringTyped);
    //L ((Runnable)factory.createItem(number)).run();
    //L  PopupMenuUtilities.addRecentlyUsedValue( valueClass, number);
    double number = Double.valueOf(stringTyped).doubleValue(); //L to compile under Java 1.4
    Double d_number = new Double(number);
    ((Runnable)factory.createItem(d_number)).run(); //L to compile under Java 1.4
    PopupMenuUtilities.addRecentlyUsedValue( valueClass, d_number); //L to compile under Java 1.4
    }
  } else if (String.class.isAssignableFrom( valueClass )) {
   Object currentValue = property.getValue();
   String currentString = "";
   if (currentValue != null) {
    currentString = currentValue.toString();
    }
   String string = (String) DialogManager.showInputDialog(I18n.getString("enterString")+":", I18n.getString("enterString"),
     javax.swing.JOptionPane.PLAIN_MESSAGE, null, null, currentString, false);
   if (string != null) {
    ((Runnable)factory.createItem( string )).run();
    PopupMenuUtilities.addRecentlyUsedValue( valueClass, string );
    }
  } else {
   AuthoringTool.getInstance().showErrorDialog( "Other... is not supported for " + valueClass.getName(), null ); // should not see this
   }
  }

 // this is not very clean
 public static void showOtherDialog (Class valueClass, Object initialValue, PopupItemFactory factory, edu.cmu.cs.stage3.alice.core.Element anchorForAnonymousItems) {
  if ((initialValue != null) && (! valueClass.isAssignableFrom( initialValue.getClass() ))) {
   initialValue = null;
   }

  if (Number.class.isAssignableFrom( valueClass )) {
   if (initialValue == null) {
    initialValue = "1";
    }
   String stringTyped = (String) DialogManager.showInputDialog(I18n.getString("enterNumber")+":", I18n.getString("enterNumber"),
     JOptionPane.PLAIN_MESSAGE, null, null, initialValue, true);
   if (stringTyped != null) {
    //L double number = Double.valueOf(stringTyped);
    //L ((Runnable)factory.createItem(number)).run();
    //L PopupMenuUtilities.addRecentlyUsedValue( valueClass, number);
    double number = Double.valueOf(stringTyped).doubleValue(); //L to compile under Java 1.4
    Double d_number = new Double(number);
    ((Runnable)factory.createItem(d_number)).run(); //L to compile under Java 1.4
    PopupMenuUtilities.addRecentlyUsedValue( valueClass, d_number); //L to compile under Java 1.4
    }
  } else if (String.class.isAssignableFrom( valueClass )) {
   String currentString = "";
   if (initialValue != null) {
    currentString = (String)initialValue;
    }
   String string = (String) DialogManager.showInputDialog(I18n.getString("enterString")+":", I18n.getString("enterString"),
     javax.swing.JOptionPane.PLAIN_MESSAGE, null, null, currentString, false);
   if (string != null) {
    ((Runnable)factory.createItem( string )).run();
    PopupMenuUtilities.addRecentlyUsedValue( valueClass, string );
    }
  } else {
   AuthoringTool.getInstance().showErrorDialog( "Other... is not supported for " + valueClass.getName(), null ); // should not see this
   }
  }

 }
